package cn.tedu.mall.order.controller;

import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.common.restful.JsonResult;
import cn.tedu.mall.order.service.IOmsCartService;
import cn.tedu.mall.order.utils.WebConsts;
import cn.tedu.mall.pojo.order.dto.CartAddDTO;
import cn.tedu.mall.pojo.order.dto.CartUpdateDTO;
import cn.tedu.mall.pojo.order.vo.CartStandardVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/oms/cart")
@Api(tags = "购物车管理模块")
public class OmsCartController {
    @Autowired
    private IOmsCartService omsCartService;

    @PostMapping("/add")
    @ApiOperation("新增购物车信息")
    //正常登录的用户在运行控制器方法前,已经在过滤器中将用户信息保存到了SpringSecurity
    //sso服务器在用户登录时,会默认将用户权限设置为"ROLE_user"
    //如果用户没有登录就无法访问这个控制器方法
    @PreAuthorize("hasAuthority('ROLE_user')")
    //@Validated注解 激活SpringValidation框架用的
    //CartAddDTO已经编写一些验证规则,如果传入的参数不符合规则会抛出BindException异常,
    public JsonResult addCart(@Validated CartAddDTO cartAddDTO){
        omsCartService.addCart(cartAddDTO);
        return JsonResult.ok("新增sku到购物车完成!");
    }

    @GetMapping("/list")
    @ApiOperation("查询购物车列表")
    @PreAuthorize("hasAuthority('ROLE_user')")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name = "page",example = "1"),
            @ApiImplicitParam(value = "每页条数",name = "pageSize",example = "3")
    })
    public JsonResult<JsonPage<CartStandardVO>> listCarts(
            // 控制器方法参数可以添加@RequestParam注解来设置默认值
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE)
            Integer page,
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE_SIZE)
            Integer pageSize){
        JsonPage<CartStandardVO> jsonPage = omsCartService.listCarts(page, pageSize);
        return JsonResult.ok(jsonPage);
    }
    @PostMapping("/delete")
    @ApiOperation("删除购物车商品")
    @ApiImplicitParam(value = "要删除的id数组",name="ids",required = true,dataType = "array")
    @PreAuthorize("hasRole('user')")
    public JsonResult removeCartsByIds (Long[] ids){
        omsCartService.removeCart(ids);
        return JsonResult.ok("删除成功!");
    }
    @PostMapping("/delete/all")
    @ApiOperation("清空购物车商品")
    @PreAuthorize("hasRole('user')")
    public JsonResult removeAllCartsByUserId (){
        omsCartService.removeAllCarts();
        return JsonResult.ok("清空成功!");
    }
    @PostMapping("/update/quantity")
    @ApiOperation("修改商品数量")
    @PreAuthorize("hasRole('user')")
    public JsonResult updateQuantity(@Validated CartUpdateDTO cartUpdateDTO){
        omsCartService.updateQuantity(cartUpdateDTO);
        return JsonResult.ok("数量修改成功!");
    }


}


















